/*
Copyright (c)2006-2007 - Brett Lajzer

See LICENSE for license information.
*/

#include <lua.h>
#include <lauxlib.h>
#include <esdl/SDL.h>
#include <esdl/SDL_ttf.h>
#include "funcs_font.h"
#include "funcs_video.h"
#include "globals.h"

//loads a font and returns a pointer to it
int l_load_font(lua_State *L){
  char fullFontFile[1024];
  TTF_Font *font;
  int ptSize = (int)lua_tonumber(L,2);
  const char* fontFile = luaL_checkstring(L,1);
  sprintf(fullFontFile,"%s/%s",gamefullpath,fontFile);
  font = TTF_OpenFont(fullFontFile, ptSize);

  lua_pushlightuserdata(L,font);
  return 1;
}

//unloads a font
int l_close_font(lua_State *L){
  TTF_CloseFont((TTF_Font*)lua_touserdata(L,1));
  lua_pushnil(L);
  return 1;
}

//gets dimensions of a string as rendered with a font
int l_font_string_metrics(lua_State *L){
  int w, h;
  TTF_SizeUTF8((TTF_Font*)lua_touserdata(L,1), luaL_checkstring(L,2), &w, &h);

  lua_pushinteger(L, w);
  lua_pushinteger(L, h);

  return 2;
}

//render a string in a font
int l_render_string(lua_State *L){
  SDL_Surface *surface;
  SDL_Color color;

  color.r = (int)lua_tonumber(L,3);
  color.g = (int)lua_tonumber(L,4);
  color.b = (int)lua_tonumber(L,5);

  surface = TTF_RenderUTF8_Blended((TTF_Font*)lua_touserdata(L,1),luaL_checkstring(L,2), color);

  //GLuint new_tex = surfaceToTexture(L, temp);
  
  //GLSurface *final = new GLSurface(new_tex, temp->w, temp->h);
  
  //SDL_FreeSurface(temp);
  
  lua_pushlightuserdata(L, surface);
  lua_pushinteger(L, surface->w);
  lua_pushinteger(L, surface->h);

  return 3;
}
